home *** CD-ROM | disk | FTP | other *** search
/ Gekkan Dennou Club 140 / Gekkan Dennou Club - 2000.1 Vol. 140 (Japan).7z / Gekkan Dennou Club - 2000.1 Vol. 140 (Japan) (Track 1).bin / tools / has060 / hanoi.s < prev    next >
Text File  |  1994-09-15  |  2KB  |  152 lines

  1. *    ~~~~~~~~~~~~
  2. *    ~~~ ハノイの塔 ~~~
  3. *    ~~~~~~~~~~~~
  4. *
  5. *    アセンブラでハノイの塔を解くためのプログラムです。
  6. *    .R 実行形式にして TYPE すると、解法が“図解”されます。
  7. *    円盤の枚数は次の N で指定します。
  8.  
  9.     N:=7
  10.  
  11. *    以下は、変更する必要はありません。
  12.  
  13.     ESC:=27
  14.  
  15. put    .macro    c
  16.         .dc.b    c
  17.     .endm
  18.  
  19. xline    .macro    n,c
  20.         .dcb.b    n,c
  21.     .endm
  22.  
  23. clear    .macro
  24.         .dc.b    ESC,'*'
  25.     .endm
  26.  
  27. locate    .macro    x,y
  28.         .dc.b    ESC,'=',' '+y,' '+x
  29.     .endm
  30.  
  31.     windowx:=96
  32.     windowy:=31
  33.  
  34.     disk:='%'
  35.     space:=' '
  36.     pole:='|'
  37.     base:='#'
  38.  
  39.     core:=2
  40.     hat:=2
  41.  
  42.     width:=N+core
  43.  
  44.     center2:=windowx/2
  45.     center1:=center2-width*2
  46.     center3:=center2+width*2
  47.  
  48.     left:=center1-width
  49.     right:=center3+width
  50.     top:=(windowy-N)/2-hat
  51.     bottom:=top+hat+N
  52.  
  53.     clear
  54.  
  55.     locate    left,bottom
  56.     xline    right-left+1,base
  57.  
  58.     i:=1
  59.     .rept    3
  60.         j:=top
  61.         .rept    bottom-top
  62.             locate    center%i,j
  63.             put    pole
  64.             j:=j+1
  65.         .endm
  66.         i:=i+1
  67.     .endm
  68.  
  69.     number1:=N
  70.     number2:=0
  71.     number3:=0
  72.  
  73.     i:=1
  74.     .rept    N
  75.         size1%i:=width-i
  76.         locate    center1-size1%i,bottom-i
  77.         xline    size1%i*2+1,disk
  78.         i:=i+1
  79.     .endm
  80.  
  81. move_disk    .macro    a,b
  82.         p:=a
  83.         q:=b
  84.         i:=number%p
  85.         j:=bottom-i
  86.         .rept N-i+hat+1
  87.             locate    center%p-size%p%i,j-1
  88.             xline    size%p%i*2+1,disk
  89.             locate    center%p-size%p%i,j
  90.             xline    size%p%i,space
  91.             put    pole
  92.             xline    size%p%i,space
  93.             j:=j-1
  94.         .endm
  95.         j:=center%p-size%p%i
  96.         locate    j,top-2
  97.         xline    size%p%i*2+1,disk
  98.         locate    j,top-1
  99.         xline    size%p%i*2+1,space
  100.         .if    center%p<center%q
  101.             .rept    center%q-center%p
  102.                 locate    j+size%p%i*2+1,top-2
  103.                 put    disk
  104.                 locate    j,top-2
  105.                 put    space
  106.                 j:=j+1
  107.             .endm
  108.         .else
  109.             .rept    center%p-center%q
  110.                 locate    j-1,top-2
  111.                 put    disk
  112.                 locate    j+size%p%i*2,top-2
  113.                 put    space
  114.                 j:=j-1
  115.             .endm
  116.         .endif
  117.         locate    j,top-1
  118.         xline    size%p%i*2+1,disk
  119.         locate    j,top-2
  120.         xline    size%p%i*2+1,space
  121.         locate    j,top
  122.         xline    size%p%i*2+1,disk
  123.         locate    j,top-1
  124.         xline    size%p%i*2+1,space
  125.         k:=number%q+1
  126.         j:=top+1
  127.         .rept N-k+hat
  128.             locate    center%q-size%p%i,j
  129.             xline    size%p%i*2+1,disk
  130.             locate    center%q-size%p%i,j-1
  131.             xline    size%p%i,space
  132.             put    pole
  133.             xline    size%p%i,space
  134.             j:=j+1
  135.         .endm
  136.         number%p:=number%p-1
  137.         number%q:=k
  138.         size%q%k:=size%p%i
  139.     .endm
  140.  
  141. hanoi    .macro    n,a,b,c
  142.         .if n>0
  143.             hanoi    n-1,a,c,b
  144.             move_disk    a,c
  145.             hanoi    n-1,b,a,c
  146.         .endif
  147.     .endm
  148.  
  149.     hanoi    N,1,2,3
  150.  
  151.     locate    0,bottom+2
  152.